﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebShopData;

namespace WebShop_Project
{
    public partial class Orderbekräftelse : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["OrderID"] == null)
            {
                Response.Redirect("http://localhost:26945/Kassa.aspx");
            }
            try
            {
                PrintDetails();
                PrintArticleDetails();
            }
            catch (Exception ex)
            {
                DBException.AddException(ex.Message, ex.GetType().ToString());
                Response.Redirect("~/ErrorPage.aspx");
            }
            
        }

        private void PrintArticleDetails()
        {
            List<ShoppingCartItem> shoppingCartItems = (List<ShoppingCartItem>)Session["ShoppingCart"];
            double sum = 0;
            foreach (var item in shoppingCartItems)
            {
                ListBox1.Items.Add(item.ToString());
                sum += (item.Article.UnitPrice * item.Quantity);
            }
            LabelTotalSum.Text = string.Format("{0:c2}", sum);
            Session.Remove("ShoppingCart");
        }

        private void PrintDetails()
        {
            var order = new Order();
            order = Orders.GetOrder((int)Session["OrderID"]);
            LabelOrderID.Text = order.OrderID.ToString();
            LabelFörnamn.Text = order.FirstName;
            LabelEfternamn.Text = order.LastName;
            LabelAdress.Text = order.Address;
            LabelPostnr.Text = order.ZipCode;
            LabelOrt.Text = order.City;
            LabelEpost.Text = order.Email;
          }
    }
}